<script type="text/javascript" src="<?=base_url()?>resource/js/layer/layer.js"></script>
<script type="text/javascript" src="<?=base_url()?>resource/js/layer/extend/layer.ext.js"></script>
<script src="<?=base_url()?>resource/js/uploadPreview.min.js" type="text/javascript"></script>
<script type="text/javascript">
    /**
     * 显示品牌列表
     * @param site
     * @param tid
     * @param custom_id
     */
    function brandShow(site,tid,custom_id) {
        layer.open({
            type: 1,
            title: '品牌列表',
            area: ['700px', '500px'], //宽高
            content: $('#brandList')
        });
        var index = layer.load(2);
        $.post("<?=blang_url('ajax/ajax/GetSapProductBrand')?>",{'site':site,'tid':tid,'custom_id':custom_id},function(data){
            if(data) {
                layer.close(index);
                var custom_id_html='';
                custom_id_html="<input type='hidden'  name='custom_id' value='" + custom_id + "'>";
                var obj = JSON.parse(data);
                var html = "<form action='<?=blang_url('productClassify/product_sap/AddSiteProductBrand')?>' method='post'>" +
                    "<input type='hidden'  name='site' value='" + site + "'>"+
                    custom_id_html+
                    "<input type='hidden'  name='tid' value='" + tid + "'>";
                for (var i = 0; i < obj.length; i++) {
                    html += "<div class='brandName'>" +
                    "<input  name='brand[]' type='checkbox' " + obj[i]['ck'] + " value='" + obj[i]['sap_id'] + "_" + obj[i]['name'] + "'>" + obj[i]['name'] + obj[i]['y_n'] +
                    "</div>"
                }
                if(obj.length!=0) {
                    $('#brandData').html(html + "<div style='margin-bottom: 20px;'><input class='btn' onclick='return CkSubmit()' type='submit' value=' 提 交 '></div></form>");
                }else{
                    $('#brandData').html("<div style='margin-top: 200px;margin-left: 250px;'>此产品类型没有任何品牌</div>");
                }
            }
        });
    }
    /**
     * 显示更新信息
     * @param id
     * @constructor
     */
    function UpdateShow(id){
        var typeLength=$('.moves').length;
        $('#remarkTr').remove();
        $('.remarkClass').remove();
        layer.open({
            type: 1,
            title: '更新产品类型信息',
            area: ['500px','610px'], //宽高
            content: $('#typeInfo')
        });
        new uploadPreview({ UpBtn: "up_file", DivShow: "logo_div", ImgShow: "logo_img" });
        var index = layer.load(2);
        $.post("<?=blang_url('ajax/ajax/GetSiteTypeInfo')?>",{'id':id},function(data){
            if(data) {
                layer.close(index);
                $('#sort_id').empty();
                var productData=JSON.parse(data);
                var obj=productData['productType'];
                var remark=productData['productRemark'];
                $('#id').val(obj.id);
                $('#site').val(obj.site);
                if(obj.is_custom==1){
                    $('#typeNameZh').show();
                    $('#typeNameTd').html("<input type='text' name='name_en' id='name_en' value='"+obj.name_en+"'>");
                }else {
                    $('#typeNameZh').hide();
                    $('#typeNameTd').html("<input type='hidden' id='name_en' value='"+obj.name_en+"'><span style='font-size: 30px;color: #009ff1'>" + obj.name_en + "</span>");
                }
                $('#name_zw').val(obj.name);
                $('#logo_img').attr('src',obj.logo);
                for(var i=1;i<=typeLength;i++){
                    $('#sort_id').append("<option value='"+i+"'>"+i+"</option>");
                }
                $('#sort_id').val(obj.sort_id);
                $('#authorizationVal').val(obj.authorization);
                var ck=[];
                switch (obj.authorization){
                    case '1':
                        ck[1]='checked';
                        break;
                    case '0':
                        ck[0]='checked';
                        break;
                    default :
                        ck[1]='checked';
                        break;
                }
                $('#shangjia').html("<input type='radio' value='1' "+ck[1]+" id='authorization_y' onclick='RadioExchange()' name='authorization'>是"+
                                    "<input type='radio' value='0' "+ck[0]+" id='authorization_n' onclick='RadioExchange()' name='authorization'>否");
                if(remark[0]['id']){
                    var color='';
                    var typeName='';
                    for(var j=0;j<remark.length;j++){
                        switch (remark[j]['type_name']){
                            case '上架':
                                color='#00CC00';
                                typeName='上';
                                break;
                            case '下架':
                                color='#ff0000';
                                typeName='下';
                                break;
                            default :
                                color='#00CC00';
                                typeName='上';
                                break;
                        }
                        var trHtml="<tr class='remarkClass' style='color: "+color+"'><td width='20%' align='center'>"+remark[j]['add_name']+"("+typeName+")"+"</td><td width='60%'>"+remark[j]['remark']+"</td><td width='20%' align='center'>"+remark[j]['time']+"</td></tr>";
                        $('#remarkTable tr').eq(j).after(trHtml);
                    }
                }
            }
        });
    }

    /**
     * 品牌提交验证
     * @returns {boolean}
     * @constructor
     */
    function CkSubmit(){
        var typeCk=$("input[name='brand[]']").is(':checked');
        if(!typeCk){
            layer.tips('请先选择品牌', '#brandData', {
                tips: [1, '#0FA6D8'] //还可配置颜色
            });
            return false;
        }
    }

    /**
     * 提交至TR验证
     * @returns {boolean}
     * @constructor
     */
    function PostTR(){
        var idsCk=$("input[name='ids[]']").is(':checked');
        var ids=new Array();
        if(!idsCk){
            layer.tips('请先选择一条数据', '#dataListTable', {
                tips: [1, '#0FA6D8'] //还可配置颜色
            });
            return false;
        }else{
            $("input[name='ids[]']:checked").each(function(){
                var val=$(this).val();
                ids.push(val);
            });
            var index=layer.load(2);
            $.post("<?=blang_url('ajax/ajax/PostTypeTomRepair')?>",{'ids':ids},function(data){
                if(data){
                    layer.close(index);
                    var str=data.split('_');
                    layer.msg(str[0],{icon: str[1]});
                }
            });
        }
    }
    //全选/全不选
    function CheckAll(){
        var checkAll=$('#checkAll').is(':checked');
        if(checkAll){
            $("input[name='ids[]']").each(function(){
                var bool=$(this).prop('disabled');
                if(bool==false){
                    $(this).prop("checked",true);
                }
            });
        }else{
            $("input[name='ids[]']").each(function(){
                var bool=$(this).prop('disabled');
                if(bool==false){
                    $(this).prop("checked",false);
                }
            });
        }
    }

    /**
     * 上下架交换
     * @constructor
     */
    function RadioExchange(){
        var radioValue  = $('input[name="authorization"]:checked').val();
        var authorizationVal=$('#authorizationVal').val();
        if(radioValue != authorizationVal){
            var upDown='下架';
            if(authorizationVal==0){
                upDown='上架';
            }
            $('#type_name').val(upDown);
            var trHtml="<tr id='remarkTr'><th>"+upDown+"备注：</th><td><input type='text' id='remark' size='40' name='remark'></td></tr>";
            $('#typeTable tr').eq(3).after(trHtml);
        }else{
            $('#remarkTr').remove();
        }
    }
    function sendInfo(){
        var radioValue  = $('input[name="authorization"]:checked').val();
        var authorizationVal=$('#authorizationVal').val();
        if(radioValue != authorizationVal) {
            var remarkTr= $.trim($('#remark').val());
            if(remarkTr==''){
                alert('上下架备注不能为空');
                return false;
            }
        }
        var name_en = $.trim($('#name_en').val());
        var name_zw = $.trim($('#name_zw').val());
        if(name_en==''){
            alert('产品类型英文不能为空');
            return false;
        }
        if(name_zw==''){
            alert('产品类型中文不能为空');
            return false;
        }
    }
</script>
<style>
    .brandName{
        background-image: url("http://oms.tomrepair.es/resource/images/m17_u3.png");
        width: 150px;
        height: 30px;
        line-height:30px;
        overflow:hidden;
        text-align: center;
        float: left;
        margin-right: 15px;
        margin-bottom: 10px;
    }
</style>

<!--菜单 start-->
<?php $this->load->view('defaults/common/left');?>
<!--菜单 end-->
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
          <h1>产品分类管理</h1>
          <?=$this->breadcrumb->generateHTML();?>
          <div class="pull-right">
            <input type="hidden" name="type" value="type">
            <input type="hidden" name="site" value="<?=$siteId?>">
            <?php if($siteISO!='OMS'){?><input  class="btn btn-oms" type="submit" onclick="return PostTR()" value="推送产品信息至<?=$siteISO?>站点"><?php }?>
             <a href="<?=blang_url('productClassify/product_sap/SiteProductList')?>" title="Back" class="btn btn-default"><i class="fa fa-reply"></i></a>
        </div>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-list"></i>产品类型列表：<span style="color: #ff0000"><?=$siteISO?></h3>
          </div>
          <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover oms-table" id="dataListTable">
                  <thead>
                    <tr>
                        <th class="text-left"><input type="checkbox" id="checkAll" onclick="CheckAll()">id</th>
                        <th class="text-left">中文名称</th>
                        <th class="text-left">英文名称</th>
                        <th class="text-left">logo</th>
                        <th class="text-right">序号</th>
                        <th class="text-right">是否上架</th>
                        <th class="text-right">子项管理</th>
                        <th class="text-right">操作</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php if($typeList): foreach ($typeList as $key=>$row):?>
                    <tr style="color: <?=$row['is_custom']?'#00CC00':''?>" >
                        <td class="text-left"><input type="checkbox" name="ids[]" <?=$row['logo']?'':'disabled'?> value="<?=$row['id']?>"><?=$row['is_custom']?$row['custom_id'].'('.$row['tid'].')':$row['tid']?></td>
                        <td class="text-left"><?=$row['name']?><?=$row['is_custom']?'('.$row['tid'].')':''?></td>
                        <td class="text-left"><?=$row['name_en']?></td>
                        <td class="text-left">
                            <img src="<?=$row['logo']?>" width="<?=275/3?>" height="<?=130/3?>">
                        </td>
                        <td class="text-right"><?=$row['sort_id']?></td>
                        <td class="text-right">
                            <?php
                               switch($row['authorization']){
                                   case '1':
                                       echo "<span style='color: #00CC00'>yes</span>";
                                       break;
                                   case '0':
                                       echo "<span style='color: #ff0000'>no</span>";
                                       break;
                                   default:
                                       break;
                               }
                            ?>
                        </td>
                        <td class="text-right">
                            <?php if($row['is_custom']){?>
                                <a class="btn btn-primary btn-sm" href="<?=blang_url('productClassify/product_sap/SiteBrandList/?site='.$siteId.'&tid='.$row['tid'].'&name='.$row['name_en'].'&custom_id='.$row['custom_id'])?>">品牌管理</a>
                            <?php }else{?>
                                <a class="btn btn-primary btn-sm" href="<?=blang_url('productClassify/product_sap/SiteBrandList/?site='.$siteId.'&tid='.$row['tid'].'&name='.$row['name_en'].'&custom_id=0')?>">品牌管理</a>
                            <?php }?>
                        </td>
                        <td class="text-right">
                            <a class="btn btn-oms btn-sm" href="javascript:void (0)" onclick="UpdateShow(<?=$row['id']?>)">更新&logo&排序</a>
                             <a class="btn btn-primary btn-sm" href="javascript:void (0)" onclick="brandShow(<?=$siteId?>,<?=$row['tid']?>,'<?=$row['is_custom']?$row['custom_id']:0?>')" title="新增品牌"><i class="fa fa fa-plus"></i></a>
                        </td>
                    </tr>

                    <?php endforeach; else:?>
                        <tr><td colspan="8" class="emptyNumber"><?=lang('public_mygdsj')?>....</td></tr>
                    <?php endif; ?>
                </tbody>
                </table>
                <?php if($pageshow):?>
                <div class="page">
                    <?=$pageshow?>
                </div>
                <?php endif;?>
            </div>
          </div>
        </div>
    </div>

    <div style="display: none" id="brandList">
        <div style="margin-top: 10px;margin-left: 20px;" id="brandData">
        </div>
    </div>
    <div style="display: none" id="typeInfo">
        <div class="ct_content" style="margin-top: 10px;margin-left: 20px;" id="infoData">
            <form action="<?=blang_url('productClassify/product_sap/UpdateTypeInfo')?>" method="post" enctype="multipart/form-data">
            <table cellspacing="0" style="border-collapse: collapse;width: 460px;" id="typeTable">
                <tr>
                    <th>产品类型(英文)：</th>
                    <td id="typeNameTd">
                    </td>
                </tr>
                <tr id="typeNameZh" style="display: none">
                    <th>产品类型(中文)：</th>
                    <td><input id="name_zw" type="text" name="name"></td>
                </tr>
                <tr>
                    <th>
                        LOGO:
                    </th>
                    <td>
                        <div id="logo_div"><img id="logo_img" width="275" height="130" /></div>
                        <input type="file" id="up_file" name="logo" />
                        <input type="hidden" id="id" name="id">
                        <input type="hidden" id="site" name="site">
                        <input type="hidden" id="type_name" name="type_name">
                        <input type="hidden" id="authorizationVal">
                    </td>
                </tr>
                <tr>
                    <th>
                        序号 :
                    </th>
                    <td>
                        <select id="sort_id" name="sort_id" style="width: 100px;">
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>
                        是否上架 :
                    </th>
                    <td id="shangjia">

                    </td>
                </tr>
                <tr align="center">
                    <td colspan="2">
                        <input type="submit" onclick="return sendInfo()" value=" 提 交 " >
                    </td>
                </tr>
            </table>
            </form>
            <div style="height: 150px;overflow :auto;margin-top: 10px;">
                <table cellspacing="0" style="border-collapse: collapse;width: 460px;" id="remarkTable">
                    <tr>
                        <th width="20%">操作人</th>
                        <th width="60%">备注</th>
                        <th width="20%">操作时间</th>
                    </tr>
                </table>
            </div>
        </div>
    </div>

</div>
